﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CurriculaAPI.POCOModel;
using CurriculaAPI.ViewModelDTO;

namespace CurriculaAPI.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Title = "Home Page";

            return View();
        }
        /// <summary>
        /// 职称名称
        /// </summary>
        /// <returns></returns>
        private List<SelectListItem> TitleNameList()
        {
            using (var db = new CurriculaEntity())
            {
                var list = db.TitleName.Select(u => new SelectListItem()
                {
                    Text = u.Name,
                    Value = u.TitleID.ToString()
                }).ToList();
                return list;
            }
        }
        /// <summary>
        /// 专业类别
        /// </summary>
        /// <returns></returns>
        private List<SelectListItem> CategoryNameList()
        {
            using (var db = new CurriculaEntity())
            {
                var list = db.CategoryName.Select(u => new SelectListItem()
                {
                    Text = u.Name,
                    Value = u.CategoryID.ToString()
                }).ToList();
                return list;
            }
        }
        /// <summary>
        /// 课程名字
        /// </summary>
        /// <returns></returns>
        private List<SelectListItem> CourseNameList()
        {
            using (var db = new CurriculaEntity())
            {
                var list = db.CourseName.Select(u => new SelectListItem()
                {
                    Text = u.Name,
                    Value = u.CourseID.ToString()
                }).ToList();
                return list;
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add()
        {
            ViewBag.TitleNameList = TitleNameList();
            ViewBag.CategoryNameList = CategoryNameList();
            ViewBag.CourseNameList = CourseNameList();
            return View();
        }
        /// <summary>
        /// 添加成功
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Add(ProfessorAddDTO dto)
        {
            if (ModelState.IsValid)
            {
                using (var db = new CurriculaEntity())
                {
                    var pro = new Professor()
                    {
                        Name = dto.Name,
                        Avatar = dto.Avatar,
                        Sex = dto.Sex,
                        CategoryName = dto.CategoryName,
                        CourseName = dto.CourseName,
                        TitleName = dto.TitleName,
                        CreateDateTime = dto.CreateDateTime
                    };
                    db.Professor.Add(pro);
                    db.SaveChanges();
                    ViewBag.msg = "添加成功";
                }
            }
            else
            {
                ModelState.AddModelError("", "添加失败");
            }
            ViewBag.TitleNameList = TitleNameList();
            ViewBag.CategoryNameList = CategoryNameList();
            ViewBag.CourseNameList = CourseNameList();
            return View(dto);
        }
        /// <summary>
        /// 教授页面
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="IsReview"></param>
        /// <param name="State"></param>
        /// <returns></returns>
        public ActionResult Default(string Name = "", int CategoryName = 0, int CourseName = 0)
        {
            using (var db = new CurriculaEntity())
            {
                var list = db.Professor.Where(p => (p.Name.Contains(Name)) && (CategoryName == 0 ? true : p.CategoryName == CategoryName) &&
               (CourseName == 0 ? true : p.CourseName == CourseName)).Select(p => new ProfessorListDTO()
               {
                   ID = p.ID,
                   Name = p.Name,
                   Avatar = p.Avatar,
                   Sex = p.Sex,
                   CategoryName = p.CategoryName1.Name,
                   CourseName = p.CourseName1.Name,
                   TitleName = p.TitleName1.Name,
                   CreateDateTime = p.CreateDateTime
               }).ToList();
                ViewBag.CategoryNameList = CategoryNameList();
                ViewBag.CourseNameList = CourseNameList();
                return View(list);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            using (var db = new CurriculaEntity())
            {
                var user = db.Professor.FirstOrDefault(s => s.ID == id);
                if (user == null)
                {
                    return Json(new { State = -1, Message = "删除失败" });
                }
                db.Professor.Remove(user);
                db.SaveChanges();
                return Json(new { State = 1, Message = "删除成功" });
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Edit(int id)
        {
            using (var db = new CurriculaEntity())
            {
                var res = db.Professor.FirstOrDefault(p => p.ID == id);
                ProfessorEditDTO ed = new ProfessorEditDTO()
                {
                    ID = res.ID,
                    Name = res.Name,
                    Avatar = res.Avatar,
                    Sex = res.Sex,
                    CategoryName = res.CategoryName,
                    CourseName = res.CourseName,
                    TitleName = res.TitleName,
                    CreateDateTime = res.CreateDateTime
                };
                ViewBag.TitleNameList = TitleNameList();
                ViewBag.CategoryNameList = CategoryNameList();
                ViewBag.CourseNameList = CourseNameList();
                return View(ed);
            }
        }
        /// <summary>
        /// 修改成功
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(ProfessorEditDTO pro)
        {
            if (ModelState.IsValid)
            {
                using (var db = new CurriculaEntity())
                {
                    var list = db.Professor.FirstOrDefault(p => p.ID == pro.ID);
                    list.Name = pro.Name;
                    list.Avatar = pro.Avatar;
                    list.Sex = pro.Sex;
                    list.CategoryName = pro.CategoryName;
                    list.CourseName = pro.CourseName;
                    list.TitleName = pro.TitleName;
                    list.CreateDateTime = pro.CreateDateTime;
                    db.SaveChanges();
                    ViewBag.msg = "编辑成功";
                }
            }
            else
            {
                ModelState.AddModelError("", "编辑失败");
            }
            ViewBag.TitleNameList = TitleNameList();
            ViewBag.CategoryNameList = CategoryNameList();
            ViewBag.CourseNameList = CourseNameList();
            return View(pro);
        }
    }
}
